use std::vec;

use lavender::*;
use lavender::db::prelude::*;
use sqlx_core::executor;
use lavender::conf::load_conf_by_file;

use crate::error::Result;
use sqlx::prelude::FromRow;

#[derive(Debug,Default,Clone,Copy,FromRow)]
pub struct FullData{
    pub id: i64,
    pub cid: i32,
    pub day: i32,
    pub time: i32,
    pub price: i32,
    pub qty: i32,
    pub r#type: i8,
}


pub struct RangeTableTest{
    pub sql_maker: Option<SqlMakerRaw>,
    pub start: i64,
    pub end: Option<i64>,
    pub field: Option<String>,
    pub order: Option<String>,
    pub limit: Option<String>,
}

impl TableRange for RangeTableTest{
    const DATABASE_NAME:&'static str = "stock";    
    fn get_range_list() -> RangeItemList{
        vec![
            range_item(0,       20200000,"kline_data_2019"),
            range_item(20200000,20210000,"kline_data_2020"),
            range_item(20210000,20220000,"kline_data_2021"),
            range_item(20220000,20230000,"kline_data_2022"),
            range_item(20230000,20240000,"kline_data_2023"),
            range_item(20240000,99999999,"kline_data_2024"),
        ]
    }
    lavender_default_table_range_property!();
}


#[tokio::test]
async fn get_test(){
    let _ = load_conf_by_file("db.json");

    // let res = lavender_model_query!(find_all lavender_query_filters!({"datenum" 20240524},{"cid" 4395}));
    // assert!(res.is_ok());    
    // let maker = res.ok().unwrap();

    // let res = RangeTableTest::with_sql_maker_and_range(maker,20200102,20230320).build_executor().unwrap().all().await;
    // assert!(res.is_ok(),"query1 failed. msg:{:?}",res.err().unwrap());  
    // println!("{:?}",res);

    let res = lavender_model_query!(find_all lavender_query_filters!({"datenum" 20240524},{"cid" 4395}));
    assert!(res.is_ok());    
    let maker = res.ok().unwrap();
    let mut tmp = RangeTableTest::with_sql_maker_and_range(maker,20200102,20220220);
    tmp.set_summary_field_sql("id").set_summary_order_sql("id  DESC").set_summary_limit_sql("10");
    let res = tmp.build_executor().unwrap().all().await;
    assert!(res.is_ok(),"query1 failed. msg:{:?}",res.err().unwrap());  
    println!("{:?}",res);

    // let res = lavender_model_query!(find_all lavender_query_filters!({"datenum" 20240524},{"cid" 4395}));
    // assert!(res.is_ok());    
    // let maker = res.ok().unwrap();
    // let mut tmp = RangeTableTest::with_sql_maker_and_point(maker,20240220);
    // tmp.set_summary_field_sql("id").set_summary_order_sql("id  DESC").set_summary_limit_sql("10");
    // let res = tmp.build_executor().unwrap().all().await;
    // assert!(res.is_ok(),"query1 failed. msg:{:?}",res.err().unwrap());  
    // println!("{:?}",res);


}


// type DeviceBase = (i32,String,i16,i16,i64,i64);

// fn get_test_mutil_insert_data(len:i32) -> Vec<DeviceBase>{
//     let mut res = vec![];
//     let tn = utils::timestamp();
//     for i in 0..len  {
//         res.push((i ,format!("sn_{}",i*i), (i % 10) as i16,(i / 10 % 10) as i16,tn,tn + (i as i64) + 1));
//     }
//     res
// }

// #[tokio::test]
// async fn test_bulk(){
//     let num = 5000;
//     let mut column = lavender_query_fields!("datenum","day","time");
//     let mutil_data = get_test_mutil_insert_data(num);
//     column.push_values_convert(mutil_data, |item|{
//         format!("{},{},{}",item.5,item.0,item.3).into()
//     });

//     let model = BaseModel::bulk_insert(column).unwrap();

//     let res = BaseTable::with_sql_maker(model).build_executor().unwrap().set_persistent(false).exec().await;
//     assert!(res.is_ok(),"query1 failed. msg:{:?}",res.err().unwrap());  
//     let insert_info = res.unwrap();
//     println!("{:?}",insert_info);
//     let insert_id = insert_info.last_insert_id();
//     let affected = insert_info.rows_affected();
//     assert!(affected == num as u64,"add bulk data failed");

//     let res = BaseTable::with_sql_maker(lavender_model_query!(delete_all lavender_query_filter!("id" > insert_id),1000).unwrap()).build_executor().unwrap().exec().await;
//     assert!(res.is_ok(),"query2 failed. msg:{:?}",res.err().unwrap());
//     let res = res.unwrap();
//     println!("delete res {:?}",res);

//     let res = BaseTable::with_sql_maker(lavender_model_query!(delete_all lavender_query_filter!("id" >= insert_id)).unwrap()).build_executor().unwrap().exec().await;
//     assert!(res.is_ok(),"query2 failed. msg:{:?}",res.err().unwrap());
//     let res = res.unwrap();
//     println!("delete res {:?}",res);
// }